package com.cho3;

import android.app.Application;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.blankj.utilcode.util.Utils;
import com.burnweb.rnwebview.RNWebViewPackage;
import com.cho3.costant.Constant;
import com.cho3.packages.CommunicationPackage;
import com.cho3.packages.DeviceInfoPakage;
import com.cho3.packages.ImagePickerPackage;
import com.cho3.packages.VpPakage;
import com.facebook.react.ReactApplication;
import com.reactnativecommunity.webview.RNCWebViewPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.leo618.zip.ZipManager;
import com.liulishuo.filedownloader.FileDownloader;
import com.oblador.vectoricons.VectorIconsPackage;
import com.puti.paylib.PayReactPackage;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {
    private String bundleParentPath = null;
    private String bundlePath = null;
    private String bundleName = "index.android.bundle";
    private File bundleFile = null;
    private static final String TAG = "MainApplication";
    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.asList(
                    new MainReactPackage(),
            new RNCWebViewPackage(),
                    new PayReactPackage(),
                    new VectorIconsPackage(),
                    new Platform(),
                    new DeviceInfoPakage(),
//                    new ImagePickerPackage(),
                    new CommunicationPackage(),
                    new RNWebViewPackage(),
                    new VpPakage()
            );
        }

        @Override
        protected String getJSMainModuleName() {
            return "index";
        }

        @Nullable
        @Override
        protected String getBundleAssetName() {
            String bundleName = "index.android.bundle";

            if (bundleFile != null && bundleFile.exists()) {
                Log.d(TAG, "assets bundle exit");
                return null;
            }
            //            Logger.d("assets bundle does not exit");
            return bundleName;
        }

        @Nullable
        @Override
        protected String getJSBundleFile() {
            bundleParentPath = Constant.getBundleUnZipPath(getApplicationContext()) + "/test";
            bundlePath = bundleParentPath + File.separator + bundleName;
            bundleFile = new File(bundlePath);
            if (bundleFile != null && bundleFile.exists()) {
                Log.d(TAG, "js bundle file " + bundleFile.getPath());
                return bundleFile.getPath();
            }
            return null;
        }


    };

    @Override
    public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SoLoader.init(this, /* native exopackage */ false);
        Utils.init(this);
        ZipManager.debug(BuildConfig.DEBUG);
        FileDownloader.setup(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
